<?php
/**
 * Created by PhpStorm.
 * User: 94004
 * Date: 2017/12/31
 * Time: 15:26
 */
header("Content-Type:text/html;charset=utf-8;");
require_once "../util/db_helper.php";
$sql = " SELECT * FROM options o WHERE o.`key`='nav_menus' ";
$res = query($sql);
$dataRes = $res[0]["value"];
$data = json_decode($dataRes);
$maxIdSql = " SELECT * FROM options o WHERE o.`key`='nav_max_id' ";
$maxIdRes = query($maxIdSql);
$maxId = $maxIdRes[0]["value"];
$_POST["id"] = $maxId + 1;
array_push($data, $_POST);
$json = json_encode($data, JSON_UNESCAPED_UNICODE);
$updateSql = " UPDATE options o SET o.value='{$json}' WHERE o.`key`='nav_menus'";
$updateRes = execute($updateSql);
$arr = array("code" => 101, "msg" => "导航链接添加失败");
if ($updateRes) {
    $maxId++;
    $updateMaxIdSql = " UPDATE options o SET o.value='{$maxId}' WHERE o.`key`='nav_max_id'";
    $updateMaxIdRes = execute($updateMaxIdSql);
    if ($updateMaxIdRes) {
        $arr["code"]=100;
        $arr["msg"] = "导航链接添加成功";
    }
}
echo json_encode($arr, JSON_UNESCAPED_UNICODE);
?>